"""
定义主页数据统计的接口视图
"""
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser
from users.models import User
from orders.models import OrderInfo
from django.utils import timezone
from datetime import timedelta

#用户总数
class UserTotalCountView(APIView):
    def get(self, request):
        #1、统计出用户的数量
        count = User.objects.count()
        cur_time = timezone.localtime()   #年月日十分秒
        #2、构建响应返回
        return Response({
            'count': count,
            'date': cur_time.date()   #年月日
        })



#日增用户
class UserDayCountView(APIView):
    def get(self, request):
        #1、获取当日０点时刻
        #当日的此时此刻，当前时间
        cur_time = timezone.localtime()
        cur_0_time = cur_time.replace(hour=0, minute=0, second=0)
        #过滤统计出当日新增的用户数量
        count = User.objects.filter(
            date_joined__gte=cur_0_time
        ).count()
        #构建响应
        return Response({
            'count': count,
            'date': cur_time.date()
        })


#日活跃用户
class UserActiveCountView(APIView):
    def get(self, request):
        #1、获取当日０点时刻
        cur_time = timezone.localtime()
        cur_0_time = cur_time.replace(hour=0, minute=0, second=0)
        #2、统计数量返回
        count = User.objects.filter(
            last_login__gte=cur_0_time
        ).count()
        return Response({
            'count': count,
            'date': cur_time.date()
        })

#日下单用户量
class UserOrderCountView(APIView):
    def get(self, request):
        #1、当日０点时刻
        cur_time = timezone.localtime()
        cur_0_time = cur_time.replace(hour=0, minute=0, second=0)
        #2、统计出当日下订单的用户数量
        users = User.objects.filter(
            orders__create_time__gte=cur_0_time
        )
        count = len(set(users))   #去重
        #3、主表入手查询
        return Response({
            'count': count,
            'date': cur_time.date()
        })



#月增用户
class UserMonthCountView(APIView):
    def get(self, request):
        #1、当日的零时刻，结束时间最后一天
        cur_time = timezone.localtime()
        end_0_time = cur_time.replace(hour=0, minute=0, second=0)
        #2、启动时间点
        start_0_time = end_0_time - timedelta(days=29)
        #３、遍历每一天０点时刻，统计新建用户数量
        ret_data = []
        for index in range(30):
            calc_0_time = start_0_time + timedelta(days=index)
            next_0_time = calc_0_time + timedelta(days=1)

            #用户新建时间，大于等于某一天的0时刻，小于次日０时刻
            count = User.objects.filter(
                date_joined__gte=calc_0_time,
                date_joined__lt=next_0_time
            ).count()
            ret_data.append({
                'count': count,
                'date': calc_0_time.date()
            })
        return Response(ret_data)



from rest_framework.generics import ListAPIView
from meiduo_admin.serializers.home_serializers import *
class GoodsDayView(ListAPIView):
    queryset = GoodsVisitCount.objects.all()
    serializer_class = GoodsVisitCountModelSerializer

    # 我们需要序列化返回的查询集是根据当日过滤出来的查询集
    def get_queryset(self):
        # 该函数，返回查询集 —— 该函数控制了后续序列化操作的目标查询集数据
        cur_0_time = timezone.localtime().replace(hour=0, minute=0, second=0)
        return self.queryset.filter(
            create_time__gte=cur_0_time
        )












